package com.wms.service.Employees;


import com.wms.common.BusinessException;
import com.wms.common.PageInfo;
import com.wms.common.Utils;
import com.wms.dao.EmployeesMapper;
import com.wms.dao.RoleMapper;
import com.wms.domain.LoginDomain;
import com.wms.domain.RoleDomain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service

public class EmployeesServiceImpl implements EmployeesService {
    @Autowired
    private EmployeesMapper mapper;

    @Autowired
    private RoleMapper roleMapper;

    private static final Logger log = LoggerFactory.getLogger(EmployeesServiceImpl.class);


    @Override
    public Object getLoginPage(LoginDomain login) {
        log.info(login.toString());
        PageInfo page = new PageInfo();
        page.setData(mapper.getLoginPage(login));
        page.setCount(login.getBaseCount());
        page.setSum(login.getBaseSum());
        return page;
    }

    @Transactional
    @Override
    public Object add(LoginDomain login) {
        log.info(login.toString());
        int value;
        login.setCreate_date(Utils.getDate());

        if(login.getId() != null){
            mapper.empUpdate(login);
        }else {
            mapper.add(login);
        }
//        Response response = new Response();
//        response.setCode(value);
        return null;
    }

    @Transactional
    @Override
    public Object deleteEmp(Long id) {
        mapper.delete(id);
//        Response response = new Response();
//        response.setCode(value);
        return null;
    }

    @Transactional
    @Override
    public Object changeEmpStat(Long id, Long emp_stat) {
        mapper.changeEmpStat(id,emp_stat);
//        Response response = new Response();
//        response.setCode(value);
        return null;
    }

    @Override
    public Object login(HttpServletRequest request,HttpServletResponse response, String id, String password) {
        Long e_id = mapper.isLogin(id,password);
        if (e_id != null){
            Cookie cookie = new Cookie("account", String.valueOf(e_id));
            cookie.setMaxAge(36000); // 设置Cookie的过期时间为1小时
            cookie.setPath("/");
            response.addCookie(cookie);
        }else{
            throw new BusinessException(201, "用户名密码不正确");
        }
        return null;
    }

    //Role

    @Override
    public Object getRolePage(RoleDomain roleDomain) {
        Long id = roleDomain.getId();
        List<RoleDomain> list = roleMapper.getRolePage(roleDomain);
//        if(id != null){
//            RoleDomain role = list.get(0);
//            List<Map<String, Object>> menuView = roleMapper.getMenu_permissionsView(id);
//            role.setMenus(menuView);
//        }
        List<RoleDomain> result = new ArrayList<>();
        for (RoleDomain role : list){
            List<Map<String, Object>> menuView = roleMapper.getMenu_permissionsView(role.getId());
            role.setMenus(menuView);
            result.add(role);
        }
        return result;
    }

    @Transactional
    @Override
    public Object addRole(RoleDomain roleDomain) {
//        Response response = new Response();
        log.info(roleDomain.toString());
        if(roleDomain.getId() !=null){
            roleMapper.roleUpdate(roleDomain);
        }else{
            roleMapper.addRole(roleDomain);
        }

        if(roleDomain.getMenu_id() != null){
            roleMapper.deleteMenu_permissionsByRole_id(roleDomain.getId());
            for (int i = 0; i < roleDomain.getMenu_id().size(); i++) {
                Long id = roleDomain.getMenu_id().get(i);
                roleMapper.addMenu_permissions(roleDomain.getId(),id);
            }
        }
//        response.setCode(1);
        return null;
    }

    @Transactional
    @Override
    public Object deleteRole(Long id) {
        log.info(String.valueOf(id));
        roleMapper.roleDelete(id);
        roleMapper.deleteMenu_permissionsByRole_id(id);
//        Response response = new Response();
//        response.setCode(1);
        return null;
    }

}
